#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>


static char pwm_path[100];

static int pwm_config(const char *attr,const char *val)
{
    char file_path[100];
    int len;
    int fd;

    sprintf(file_path,"%s/%s",pwm_path,attr);
    if(0>(fd=open(file_path,O_WRONLY))){
        perror("open error");
        return fd;
    }
    len = strlen(val);
    if(len!=write(fd,val,len)){
        perror("write error");
        close(fd);
        return;
    }
    close(fd);
    return 0;
}

int main(int argc, char*argv[])
{
    if(4!=argc){
        fprintf(stderr,"usage:%s<id><period><duty>\n",argv[0]);
        exit(-1);
    }
    printf("PWM config:id<%s>,period<%s>,duty<%s>\n",argv[1],argv[2],argv[3]);
    sprintf(pwm_path,"sys/class/pwm/pwmchip%s/pwm0",argv[1]);

    if(access(pwm_path,F_OK)){
        char temp[100];
        int fd;

        sprintf(temp,"/sys/class/pwm/pwmchip%s/export",argv[1]);
        if(0>(fd = open(temp,O_WRONLY))){
            perror("open error");
            exit(-1);
        }
        if(1!=write(fd,"0",1)){
            perror("write error");
            close(fd);
            exit(-1);
        }
        close(fd);
    }
    if(pwm_config("period",argv[2]))
        exit(-1);
    if(pwm_config("duty_cycle",argv[3]))
        exit(-1);

    pwm_config("enable","1");
    exit(0);
}







